package dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import model.Folder;
import model.Membership;
import model.Resource;
import model.User;

@Stateless
public class ResourceDao {

	@PersistenceContext
	EntityManager em;

	/**
	 * @author tokea
	 * @param repId
	 * @return List<Resource>
	 */
	public List<Resource> getResourceListFromRepository(int repId) {
		List<Resource> rlist = new ArrayList<Resource>();
		try {
			String query = "SELECT r FROM Resource r";
			List<Resource> subList = em.createQuery(query, Resource.class)
					.getResultList();
			for (Resource r : subList)
				if (r.getRepository().getId() == repId)
					rlist.add(r);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return rlist;
	}

	/**
	 * @author Zsombor+tokea
	 * @param r
	 */
	public void persistResource(Resource r) {
		em.persist(r);
		em.flush();
		em.refresh(r);
	}
	
	/**
	 * @author Zsombor
	 * @param id
	 * @return Resource
	 */
	
	public Resource getResourceById(int id){
		return em.find(Resource.class, id);
	}

	/**
	 * @author fodorl + soreas
	 * @return List<Resource> returneaza toata resursele din baza de date
	 */

	public List<Resource> getAllResources() {
		List<Resource> resources = em.createQuery("SELECT r FROM Resource r",
				Resource.class).getResultList();
		return resources;
	}

	/**
	 * @author Zsombor
	 * @return String returns content of a given resource
	 */

	public String getContent(int id) {
		Resource resource = em.find(Resource.class, id);

		if (resource != null) {
			return resource.getContent();
		} else
			return "";
	}

	/**
	 * @author Zsombor
	 * @param int id
	 * @param String
	 *            modifies content of resource
	 */

	public void modifyContent(int id, String newContent) {
		Resource resource = em.find(Resource.class, id);
		resource.setContent(newContent);
		em.persist(resource);
	}

	/**
	 * @author Zsombor +tokea
	 * @param int resId
	 * 
	 */
	public void deleteResource(int resId) {
		Resource r;
		r = em.find(Resource.class, resId);
		if (r != null) {
			Folder f = em.find(Folder.class, resId);
			em.remove(em.merge(f));
		}
	}

	/**
	 * @author Zsombor + tokea
	 * @param reDto
	 */
	public void unlockResource(int resId) {
		Resource r;
		r = em.find(Resource.class, resId);
		if (r != null) {
			r.setLockUser(em.find(User.class, 1));
			em.persist(r);
		}

	}

	/**
	 * @author tokea
	 * @param parentId
	 * @param name
	 * @return boolean unique
	 */
	public boolean verifyNameUniqueInFolder(int parentId, String name) {
		boolean unique = true;
		String query = "SELECT r FROM Resource r WHERE r.folder.id = :parentId AND r.name = :name";
		List<Resource> flist = em.createQuery(query, Resource.class)
				.setParameter("parentId", parentId).setParameter("name", name)
				.getResultList();
		if (flist.size() > 0)
			unique = false;
		return unique;
	}

	/**
	 * @author tokea
	 * @param idRes
	 * @return boolean locked
	 */
	public boolean verifyResourceLocked(int idRes, int idUser) {
		boolean locked = false;
		Resource res = em.find(Resource.class, idRes);
		if (res.getLockUser().getId() != 1 && res.getLockUser().getId() != idUser)
			locked = true;
		return locked;
	}

	/**
	 * @author tokea
	 * @param idRes
	 * @return boolean locked
	 */
	public boolean verifyResourceLockedByUser(int idRes, int idUser) {
		boolean locked = false;
		Resource res = em.find(Resource.class, idRes);
		if (res.getLockUser().getId() != idUser)
			locked = true;
		return locked;
	}
	
	/**
	 * @author tokea
	 * @param idRes
	 * @param idUser
	 * @return boolean success
	 */
	public boolean lockResource(int idRes, int idUser) {
		boolean success = false;
		Resource res = em.find(Resource.class, idRes);
		Folder repository = res.getRepository();
		User user = em.find(User.class, idUser);
		String query = "SELECT m FROM Membership m WHERE m.user.id = :idUser AND m.folder.id = :idRep";
		List<Membership> mem = em.createQuery(query, Membership.class)
				.setParameter("idUser", idUser)
				.setParameter("idRep", repository.getId()).getResultList();
		if (res.getLockUser().getId() == 1 && mem.size() > 0) {
			res.setLockUser(user);
			em.persist(res);
			success = true;
		}
		return success;
	}
	/**
	 * @author fodorl
	 * @param idResource
	 * @return String
	 * returneaza numele resursei cu id-ul dat
	 */

	public String getNameById(int idResource) {
		Folder resource = em
				.createQuery("SELECT r from Folder r where r.id = ?1",
						Folder.class).setParameter(1, idResource)
				.getSingleResult();
		return resource.getName();

	}
}
